import api from "../../config/api";
import request from "../../utils/request";
import { jump, stringify } from "../../utils/utils"

// components/recycle-reserve/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 回收类型：大件: bulky 家电: appliances 衣物: clothing
    type: {
      type: String,
      value: 'bulky'
    },
    // 隐藏喇叭通知
    hideInform:{
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 物品列表
    categoryList: [],
    // 选中物品列表
    selectedList: [],
    // 价格统计
    sumPrice: '',
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    // 初始化
    attached() {
      this.getThing()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取所有物品
     */
    async getThing() {
      let res = {}
      switch (this.data.type) {
        // 大件
        case 'bulky':
          res = await request.get(api.bulkyThing)
          break;
        // 家电
        case 'appliances':
          res = {
            httpStatus: 200,
            data: [
              { goodsName: '电视机', price: 0 },
              { goodsName: '空调', price: 0 },
              { goodsName: '冰箱', price: 0 },
              { goodsName: '电脑', price: 0 },
              { goodsName: '其他', price: 0 },
            ]
          }
          break;
        // 衣物
        case 'clothing':
          res = {
            httpStatus: 200,
            data: [
              { goodsName: '羽绒服', price: 0 },
              { goodsName: '卫衣', price: 0 },
              { goodsName: '外套', price: 0 },
              { goodsName: '皮草', price: 0 },
              { goodsName: '下装', price: 0 },
              { goodsName: '鞋子', price: 0 },
              { goodsName: '大衣', price: 0 },
              { goodsName: '其他', price: 0 },
            ]
          }
          break;
        default:
          break;
      }
      console.log('物品：', res)

      if (res.httpStatus == 200) {
        res.data.map((item) => {
          item.value = 1
          item.selected = false
        })

        this.setData({
          categoryList: res.data
        })
      }
    },
    /**
     * 选择种类
     */
    handleSelected(e) {
      const { categoryList } = this.data;
      const { dataset = {} } = e.currentTarget;

      const list = categoryList.map(item => (
        {
          ...item,
          selected: item.goodsName === dataset.name ? !item.selected : item.selected,
          value: item.goodsName === dataset.name ? 1 : item.value,
        }
      ));
      const selectedList = list.filter(item => item.selected);

      this.getSumPrice(selectedList);

      this.setData({
        categoryList: list,
        selectedList,
      })
    },
    /**
     * 清空已选择的物品
     */
    handleEmpty() {
      const { categoryList } = this.data;

      const list = categoryList.map(item => (
        {
          ...item,
          selected: false,
          value: 1,
        }
      ))

      this.setData({
        categoryList: list,
        selectedList: [],
        sumPrice: 0
      })
    },
    /**
     * 减少/添加所选物品的数量
     */
    handleChange(e) {
      const { categoryList, selectedList } = this.data;
      const { dataset: { item = {}, option } } = e.currentTarget;
      
      if(option === 'subtraction' && item.value === 1){
        return
      }

      const cList = this.handleChangeNumber(categoryList, option, item);
      const sList = this.handleChangeNumber(selectedList, option, item);

      this.getSumPrice(sList);

      this.setData({
        categoryList: cList,
        selectedList: sList,
      })
    },
    /**
     * 选中物品的数量变化
     */
    handleChangeNumber(list, option, item) {
      const data = list.map(sl => (
        {
          ...sl,
          value: sl.goodsName === item.goodsName ? (option === 'plus' ? (sl.value + 1) : (sl.value - 1)) : sl.value
        }
      ))
      return data
    },
    /**
     * 计算价格
     */
    getSumPrice(list) {
      let sumPrice = 0
      const { categoryList } = this.data
      list.map(item => {
        categoryList.map(sc => {
          if (item.goodsName === sc.goodsName) {
            sumPrice += item.value * item.price
          }
          return sc
        })
        return item
      })

      this.setData({
        sumPrice
      })
    },
    /**
     * 下一步
     */
    goNext() {
      const { selectedList, sumPrice, type } = this.data;
      if (selectedList.length <= 0) {
        return
      }
      jump('recycleFurnitureInfo', {
        obj: stringify({
          selectedList,
          sumPrice,
          type
        })
      })
    },
  }
})
